<?php
    /**
     * @desc add session flash variable handling in this plugin
     * @author xq262144 by xq262144@gmail.com
     */

     final class SessionFlash
     {
         /**
          * @desc flash var handling classs
          */
         private static $_init = false;
         private static $_var = null;

         /**
          * @desc handling flash variable setting
          * @param string $name name of flash variable to be set
          * @param mixed $var value of flash variable to be set
          */
         final public static function setFlash( $name, $var )
         {
             self::initFlash();

             $name = trim( strval( $name ) );
             $_SESSION['flash'][$name] = $var;
         }

         /**
          * @desc handling flash variable getting
          * @param string $name name of flash variable to be get
          * @return mixed value of flash variable
          */
         final public static function getFlash( $name )
         {
            self::initFlash();
            $name = trim( strval( $name ) );
            if ( array_key_exists( $name, self::$_var ) )
            {
                return self::$_var[$name];
            }
            else return null;
         }

         /**
          * @desc handling flash variable init
          */
         final public static function initFlash()
         {
             if ( !self::$_init )
             {
                 $_init = true;
                 if ( !isset( $_SESSION ) ) session_start();
                 if ( isset( $_SESSION['flash'] ) )
                 {
                    self::$_var = $_SESSION['flash'];
                    if ( empty( self::$_var ) || !is_array( self::$_var ) )
                        self::$_var = array();
                 }
                 $_SESSION['flash'] = array();
             }
             else
                 return false;
         }
     }